package com.bsoft.gol.hbs.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import hcn.base.ServBaseConf;
import org.atag.dao.action.QoHibernateStatelessResultAction;
import org.atag.dao.ext.HibernateExtendDAO;
import org.atag.dao.model.SimplePageInfo;
import org.springframework.util.StringUtils;
import com.bsoft.gol.hbs.service.impl.sysroute.dto.SystemConfigListDTO;
import com.bsoft.gol.hbs.service.impl.sysroute.vo.SystemConfigListVO;

/**
 * 外部系统基础配置DAO
 *
 * @author neoLin - 2022/09/21 14:06
 **/
public abstract class ServBaseConfDAO extends HibernateExtendDAO<ServBaseConf> {


    @DAOMethod(sql = "FROM ServBaseConf WHERE tenantId=:tenantId AND orgCd=:orgCd AND sysType=:sysType AND active=1 ")
    public abstract ServBaseConf getByManageUnit(@DAOParam("tenantId") String tenantId, @DAOParam("orgCd") String orgCd
            , @DAOParam("sysType") String sysType);

    /**
     * 查询系统配置列表
     */
    public SimplePageInfo<SystemConfigListVO> getSysConfigList(SystemConfigListDTO dto) {
        QoHibernateStatelessResultAction<SystemConfigListVO, SystemConfigListDTO> action =
                new QoHibernateStatelessResultAction<>(true, dto, SystemConfigListVO.class);
        String baseSql = "SELECT {{config_id AS configId,tenant_id AS tenantId,org_cd AS orgCd,config_type AS configType," +
                "sys_type AS sysType,service_url AS serviceUrl,service_version AS serviceVersion,protocol AS protocol," +
                "active AS active,op_name AS opName,update_time AS updateTime,content_type AS contentType," +
                "contact_url AS contactUrl, use_tpl AS useTpl }} " +
                " FROM serv_base_conf WHERE 1=1 ";
        action.setSql(baseSql);
        action.addCondition(" AND tenant_id=:tenantId ");
        action.addCondition(" AND org_cd=:orgCd ");
        action.addCondition(" AND sys_type=:sysType ");
        action.addCondition(" AND active=:active ");
        String orgCd = dto.getOrgCd();
        if (StringUtils.isEmpty(orgCd)) {
            action.addSql(" AND config_type='1' ");
        }
        HibernateSessionTemplate.instance().execute(action);
        return action.getSimpleResultPage();

    }
}
